﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_UpdateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 50)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if(!IsPostBack)
        {
            //Response.Write(".");
            string loginName = Request.QueryString["loginName"].ToString();
            UserInfo ui = UserInfoManager.FindByLoginName(loginName);
            TarreOA.TarreOAModels.User u = UserManager.FindByLoginName(loginName);
            lblLoginNameText.Text = u.LoginName;
            lblSection.Text = u.Section.SectionName;
            txtUserName.Text = u.UserName;
            txtTelephone.Text = ui.Telephone;
            if (ui.Sex == true)
            {
                ddlSex.SelectedIndex = 0;
            }
            else 
            {
                ddlSex.SelectedIndex = 1;
            }
            txtAddress.Text = ui.Address;
            txtIDCard.Text = ui.IDCard;
            mob.Text = ui.Mob;
            txtFinishShool.Text = ui.FinishShool;
            if (ui.FinishShoolTime != null && ui.FinishShoolTime.ToShortDateString() != "0001/1/1")
            {
                txtFinishShoolTime.Text = ui.FinishShoolTime.ToShortDateString();
            }
            txtEmail.Text = ui.Email;
            txtWage.Text = u.Wage.ToString();
            ddlJob.Text = u.Job.JobName;
            ddlPopedomGroup.SelectedValue = u.PopedomGroup.PopedomGroupId.ToString();
            ddlAcademicCertificate.SelectedValue = ui.AcademicCertificate.ToString();
            ddlSex.SelectedValue = ui.Sex.ToString();
            ddlNation.SelectedValue = ui.Nation.NationId.ToString();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        TarreOA.TarreOAModels.User user = new User();
        user.LoginName = lblLoginNameText.Text;
        user.UserName = txtUserName.Text.Trim();
        PopedomGroup popedomGroup = new PopedomGroup();
        popedomGroup.PopedomGroupId=Convert.ToInt32(ddlPopedomGroup.SelectedValue);
        user.PopedomGroup = popedomGroup;
        UserInfo userInfo = new UserInfo();
        userInfo.AcademicCertificate = Convert.ToInt32(ddlAcademicCertificate.SelectedValue);
        Nation nation = new Nation();
        nation.NationId = Convert.ToInt32(ddlNation.SelectedValue);
        userInfo.Nation = nation;
        userInfo.Sex = Convert.ToBoolean(ddlSex.SelectedValue);
        userInfo.Telephone = txtTelephone.Text.Trim();
        userInfo.Address = txtAddress.Text.Trim();
        userInfo.Email = txtEmail.Text.Trim();
        
        userInfo.Mob = mob.Text.Trim();
        userInfo.FinishShool = txtFinishShool.Text.Trim();
        userInfo.IDCard = txtIDCard.Text.Trim();
        //Job job = new Job();
        //job.JobId = Convert.ToInt32(ddlJob.SelectedValue);
        //user.Job = job;
        user.Wage = Convert.ToDecimal(txtWage.Text);
        if (txtFinishShoolTime.Text != null && txtFinishShoolTime.Text !="")
        {            userInfo.FinishShoolTime = Convert.ToDateTime(txtFinishShoolTime.Text);
        }
        if (UserManager.UpdateUser(user,userInfo))
        {
            ClientScript.RegisterStartupScript(this.GetType(),"","alert('修改成功！')",true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改失败！请重试！')", true);
        }
    }
}
